package com.yanchangapp.ui.activity.mail;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BasePresenter;
import com.yanchangapp.R;
import butterknife.BindView;

import static com.yanchangapp.commom.AppConstant.LOOK_ATTACHMENT;

public class AttachmentShowActivity extends BaseActivity {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.webView)
    WebView mWebView;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.llProgressBar)
    LinearLayout llProgressBar;
    private boolean isError = false;
    private static final String TAG = "AttachmentShowActivity";
    private static final String ATTACHMENT_PATH = "ATTACHMENT_PATH";
    private String mAttachmentPath;

    public static void show(Activity activity,String path){
        Intent intent = new Intent(activity,AttachmentShowActivity.class);
        intent.putExtra(ATTACHMENT_PATH,path);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
       if (getIntent() != null){
           mAttachmentPath = getIntent().getStringExtra(ATTACHMENT_PATH);
           if (TextUtils.isEmpty(mAttachmentPath)){
               finish();
               return;
           }
           if (!mAttachmentPath.endsWith("png") || !mAttachmentPath.endsWith("txt") ||
           !mAttachmentPath.endsWith("jpg") || !mAttachmentPath.endsWith("jpeg") ||
           !mAttachmentPath.endsWith("bmp") || !mAttachmentPath.endsWith("gif")){
               // 如果是以这些后缀结尾的文件
               mAttachmentPath =  LOOK_ATTACHMENT + mAttachmentPath;
           }
           Log.e(TAG, "init: "+ mAttachmentPath);
       }else {
           finish();
       }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.getSettings().setLightTouchEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null){
            mWebView.destroy();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_attachment_show;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("附件详情");
        initWebView();
        showLoadingView();
    }

    private void initWebView() {
        WebSettings setting = mWebView.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);
        setting.setBuiltInZoomControls(true);
        setting.setSupportZoom(true);
        setting.setDisplayZoomControls(false);
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        setting.setJavaScriptCanOpenWindowsAutomatically(true);
        setting.setAppCacheEnabled(true);
        setting.setDomStorageEnabled(true);
        setting.setGeolocationEnabled(true);
        setting.setAppCacheMaxSize(Long.MAX_VALUE);
        setting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        setting.setAllowFileAccess(true);
        setting.setAllowFileAccessFromFileURLs(true);
        setting.setAllowUniversalAccessFromFileURLs(true);
        setting.setAllowContentAccess(true);
        setting.setBlockNetworkImage(true);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                isError = false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!isError) {
                    showContentView();
                    isError = false;
                    return;
                }
                showErrorView();
                isError = false;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                isError = true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100){
                    // 展示内容
                    showContentView();
                    view.getSettings().setBlockNetworkImage(false);
                }else {
                    showLoadingView();
                }

            }
        });
    }

    // 进行页面数据加载
    private void loadWebView(){
        mWebView.loadUrl(mAttachmentPath);
    }

    @Override
    protected void initData() {
        loadWebView();
    }

    private void showContentView(){
        if (mWebView.getVisibility() == View.GONE){
            mWebView.setVisibility(View.VISIBLE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            llErrorNetwork.setVisibility(View.GONE);
        }

        if (llProgressBar.getVisibility() == View.VISIBLE){
            llProgressBar.setVisibility(View.GONE);
        }
    }

    private void showErrorView(){
        if (mWebView.getVisibility() == View.VISIBLE){
            mWebView.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.GONE){
            llErrorNetwork.setVisibility(View.VISIBLE);
        }

        if (llProgressBar.getVisibility() == View.VISIBLE){
            llProgressBar.setVisibility(View.GONE);
        }
    }

    private void showLoadingView(){
        if (mWebView.getVisibility() == View.VISIBLE){
            mWebView.setVisibility(View.GONE);
        }

        if (llErrorNetwork.getVisibility() == View.VISIBLE){
            llErrorNetwork.setVisibility(View.GONE);
        }

        if (llProgressBar.getVisibility() == View.GONE){
            llProgressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        btnNextRefresh.setOnClickListener(v -> {
            showLoadingView();
            loadWebView();
        });

    }

}
